import {getRegionTree} from "../api/business.js";
import {onMounted} from "vue";

export const useRegionTree = () => {
  const regionTreeRaw = ref([])
  const regionTree = ref([]) // 根据实际类型调整

  const loading = ref(false)
  const fetchData = async () => {
    loading.value = true
    try {
      const res = await getRegionTree()

      if (res.code == 200) {
        regionTreeRaw.value = res.data[0]?.childrens

        let area = res.data[0]?.childrens
        //   .map(item => {
        //   return {
        //     text: item.label,
        //     value: item.id,
        //   }
        // })

        regionTree.value = area

      } else {
        regionTree.value = []
      }


    } catch (error) {
      console.error('获取区域树失败:', error)
    } finally {
      loading.value = false
    }
  }


  onMounted(() => {
    fetchData()
  })

  return {
    regionTreeRaw,
    regionTree,
    loading,
    fetchData // 也可以暴露方法用于手动刷新
  }
}
